package com.xzz.mybatis.generator.common;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;

/**
 * 所有接口必须返回的reponse对象
 *
 */
@Data
@Slf4j
public final class MyResponseStatus {

    private String code = SUCCESS_CODE;
    private String msg = SUCCESS_MSG;
    private Object data;

    public static final String SUCCESS_CODE = "1";
    public static final String SUCCESS_MSG = "操作成功";
    public static final String FAIL_CODE = "-1";
    public static final String FAIL_MSG = "服务器错误";
    public static final String DUPLICATE_KEY_FAILURE = "错误!含重复数据";

    private MyResponseStatus() {
    }

    public static ResponseStatusBuilder newBuilder() {
        return new ResponseStatusBuilder();
    }

    public static class ResponseStatusBuilder {

        private MyResponseStatus responseStatus;

        private ResponseStatusBuilder() {
            responseStatus = new MyResponseStatus();
        }

        public ResponseStatusBuilder code(String code) {
            this.responseStatus.setCode(code);
            return this;
        }

        public ResponseStatusBuilder msg(String msg) {
            this.responseStatus.setMsg(msg);
            return this;
        }

        public ResponseStatusBuilder data(Object data) {
            this.responseStatus.setData(data);
            return this;
        }

        public ResponseStatusBuilder paging(Long count, Object records) {
            this.responseStatus.setData(new PagingResponseData(count, records));
            return this;
        }

        public ResponseStatusBuilder pagingWithSum(Long count, Object records, Object sums) {
            this.responseStatus.setData(new PagingWithSumResponseData(count, records, sums));
            return this;
        }

        public ResponseStatusBuilder pagingWithSum(PagingWithSumResponseData withSumResponseData) {
            this.responseStatus.setData(withSumResponseData);
            return this;
        }

        public MyResponseStatus build() {
            return this.responseStatus;
        }

        public MyResponseStatus quickBuildSuccess() {
            return code(SUCCESS_CODE).msg(SUCCESS_MSG).build();
        }

        /**
         * 返回单个实体对象
         */
        public MyResponseStatus quickBuildSingleSuccess(Object data) {
            return code(SUCCESS_CODE).msg(SUCCESS_MSG).data(data).build();
        }

        public MyResponseStatus quickBuildSuccess(String code, String msg) {
            return code(code).msg(msg).build();
        }

        public MyResponseStatus quickBuildSuccess(String code, String msg,Object data) {
            return code(code).msg(msg).data(data).build();
        }

        /**
         * 返回分页对象, data一般是list对象
         */
        public MyResponseStatus quickBuildPagingSuccess(Long count, Object records) {
            return code(SUCCESS_CODE).msg(SUCCESS_MSG).paging(count, records).build();
        }

        /**
         * 返回分页对象带分页合计和总计, data一般是list对象, sums是普通bean对象,包含字段当前分页合计和总合计
         */
        public MyResponseStatus quickBuildPagingWithSumSuccess(Long count, Object records, Object sums) {
            return code(SUCCESS_CODE).msg(SUCCESS_MSG).pagingWithSum(count, records, sums).build();
        }

        /**
         * 返回分页对象带分页合计和总计
         *
         * @param withSumResponseData data属性一般是list对象, sums属性是普通bean对象,包含字段当前分页合计和总合计
         */
        public MyResponseStatus quickBuildPagingWithSumSuccess(PagingWithSumResponseData withSumResponseData) {
            return code(SUCCESS_CODE).msg(SUCCESS_MSG).pagingWithSum(withSumResponseData).build();
        }

        public MyResponseStatus quickBuildFailure() {
            return code(FAIL_CODE).msg(FAIL_MSG).build();
        }

        public MyResponseStatus quickBuildFailure(String msg) {
            return code(FAIL_CODE).msg(msg).build();
        }

        /**
         * 唯一索引失败返回提示
         *
         * @return
         */
        public MyResponseStatus quickBuildDuplicateKeyFailure(DuplicateKeyException e) {
            log.error("数据重复异常", e);
            return code(FAIL_CODE).msg(DUPLICATE_KEY_FAILURE).build();
        }

        public MyResponseStatus quickBuildFailure(String code, String msg) {
            return code(code).msg(msg).build();
        }

        public MyResponseStatus quickBuildFailure(String code, String msg,Object data) {
            return code(code).msg(msg).data(data).build();
        }
    }
}
